//
//  STOrderHistoryViewController.swift
//  SeaTransportManage
//
//  Created by guopengwen on 2021/11/26.
//

import UIKit
import HMSegmentedControl
import Foundation

class STOrderHistoryViewController: GPBaseViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {

    var controllers: Array<STOrderHistoryContentViewController> = Array()
    var gl_currentIndex: Int = 0
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setNavBarTitle("订单管理")
        initData()
        addSubviewForPage()
    }
    
    func initData() {
        var i = 0
        while (i < 2) {
            let vc: STOrderHistoryContentViewController = STOrderHistoryContentViewController.init()
            vc.type = i
            controllers.append(vc)
            i += 1;
        }
    }

    func addSubviewForPage() {
        addTopBGView()
        
        view.addSubview(segmentedControl)
        segmentedControl.addTarget(self, action: #selector(segmentedControlChangedValue(sender:)), for: .valueChanged)
        
        pageController.setViewControllers([controllers[0]], direction: UIPageViewController.NavigationDirection.forward, animated: true) { (b: Bool) in
            self.addChild(self.pageController)
            self.view.addSubview(self.pageController.view)
            self.pageController.didMove(toParent: self)
        }
    }
    
    @objc func segmentedControlChangedValue(sender: Any) {
        let segmentControl: HMSegmentedControl = sender as! HMSegmentedControl
        let index: Int  = Int(segmentControl.selectedSegmentIndex)
        if(index == 0) {
            pageController.setViewControllers([controllers[0]], direction: UIPageViewController.NavigationDirection.reverse, animated: true) { (b: Bool) in
            }
        } else {
            pageController.setViewControllers([controllers[1]], direction: UIPageViewController.NavigationDirection.forward, animated: true) { (b: Bool) in
            }
        }
    }
    
    // UIPageViewControllerDelegate, UIPageViewControllerDataSource
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        let vc: STOrderHistoryContentViewController = viewController as! STOrderHistoryContentViewController
        if(vc.type == 0) {
            return nil
        }
        let index: Int = vc.type - 1
        return controllers[index]
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        let vc: STOrderHistoryContentViewController = viewController as! STOrderHistoryContentViewController
        if(vc.type == 1) {
            return nil
        }
        let index: Int = vc.type + 1
        return controllers[index]
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
        let vc: STOrderHistoryContentViewController = pendingViewControllers.first as! STOrderHistoryContentViewController
        gl_currentIndex = vc.type
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        if(completed) {
            segmentedControl.setSelectedSegmentIndex(UInt(gl_currentIndex), animated: true)
        }
    }
    
    // 懒加载
    
    lazy var pageController: UIPageViewController = {
        let pageVC = UIPageViewController.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: [UIPageViewController.OptionsKey.spineLocation:NSNumber(value:UIPageViewController.SpineLocation.min.rawValue)])
        pageVC.view.frame = CGRect.init(x: 0, y: 64, width: view.bounds.size.width, height: view.bounds.size.height - 64)
        pageVC.delegate = self
        pageVC.dataSource = self
        return pageVC
    }()
    
    lazy var segmentedControl: HMSegmentedControl = {
        let segmentControl = HMSegmentedControl(sectionTitles: ["出口订单", "进口订单"])
        segmentControl.titleTextAttributes = [ NSAttributedString.Key.foregroundColor: UIColor.white ]
        segmentControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocation.down;
        segmentControl.selectionStyle = HMSegmentedControlSelectionStyle.textWidthStripe;
        segmentControl.backgroundColor = UIColor.init(hexString: "#FFFFFF", alpha: 0)
        segmentControl.selectionIndicatorColor = UIColor.white;
        segmentControl.frame = CGRect(x: 0, y: 10, width: kScreenWidth, height: 40)
        return segmentControl
    }()
}
